<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>
    <script src="/static/js/myfun.js"></script>
    <title>耐蚀钢报名表</title>
    <style>
        .container {
            width: 1300px;
            margin: 40px auto;
        }

        .table {
            margin-top: 30px;
        }

        .table,
        th {
            text-align: center;
        }

        .input-group {
            width: 500px;
        }

        .page_navigation {
            float: right;
        }

        /* #qrcode img {
            margin: 0 auto;
        } */
        .c-red {
            color: red;
        }
    </style>

</head>

<body>
    <div class="container">
        <div style="position: relative;">
            <div class="input-group" >
                <input type="text" class="form-control" placeholder="Search for 姓名 or 单位 or 电话" id="key" name="key"
                    value="<?php echo isset($key)? $key : '';?>" style="width: 300px; height: auto;">
                <span class="input-group-btn">
                    <button class="btn btn-default" type="button" onclick="search()">查询</button>
                    <button class="btn btn-default" type="button" onclick="celerSearch()">清除</button>
                    <button class="btn btn-primary " data-toggle="modal" data-target="#myModal"
                        style="background-color: pink;color: white;margin-left: 10px;">新增</button>
                        <!-- <button class="btn btn-primary" onclick="printReceipt()" style="margin-left: 10px;">打印收据</button> -->
                    <a class="btn btn-primary" href="/admin/index/export" style="background-color: rgb(26, 127, 241);color: white;margin-left: 10px;">导出</a>
                </span>
            </div>
            <span class="input-group-btn" style="position: absolute;top:0;right: 180px;">
                <button class="btn btn-primary" type="button" data-toggle="modal" data-target="#reModal">修改密码</button>
                <button class="btn btn-default" type="button" onclick="logout()"><?php echo $username;?> 退出</button>
            </span>
        </div>
        <table class="table table-hover table-bordered">
            <tr class="active">
                <th>选择</th>
                <th>id</th>
                <th style="width: 100px;">姓名</th>
                <th>所在机构</th>
                <th>电话</th>
                <th>费用类型</th>
                <th>是否签到</th>
                <th>是否缴费</th>
                <th>金额</th>
                <th>操作</th>
            </tr>
            <?php
                $feeType = [1=>'普通', 2=>'学生'];
                $memberType = [10=>'正常', 90=>'免注册'];
                foreach ($data['list'] as $val) {
            ?>
            <tr class="data-row">
                <td><input type="checkbox" name="checkbox[]" value="<?php echo $val['id']?>"></td>
                <td><?php echo $val['id']?></td>
                <td><?php echo $val['truename']?></td>
                <td><?php echo $val['organization']?></td>
                <td><?php echo $val['phone']?></td>
                <td><?php echo $feeType[$val['fee_type']]?></td>
                <td><?php echo $val['status'] ? '是' : '否'; ?></td>
                <td><?php echo $val['is_pay'] ? '是' : '否'; ?></td>
                <td><?php echo $val['money']?></td>
                <td class="td-manager">
                    <!-- <a href="javascript:;" onclick="doPrint(<?php echo $val['id'];?>)">打印</a> -->
                    <a href="/admin/index/info/id/<?php echo $val['id'];?>">详细信息</a>
                    <a href="javascript:;" onclick="update(<?php echo $val['id'];?>)">修改</a>
                    <a href="javascript:;" onclick="del(<?php echo $val['id'];?>, this)">删除</a>
                </td>
            </tr>
            <?php
                }
            ?>
        </table>
        <div>
            <nav aria-label="Page navigation" class="page_navigation">
                <ul class="pagination">
                    <?php
                        $total = $data['total'];
                        $page = $data['page'];
                        $limit = $data['limit'];
                        $pages = intval($total/$limit) + 1;
                    ?>
                    <li>
                        <a href="?page=<?php echo ($page-1)==0 ? 1 : $page-1; ?><?php echo isset($key) ? '&key='.$key : '';?>" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <?php
                        $li = (($pages - $page) >= 5 ? 4 : $pages - $page) + 1; 
                        for ($i=0;$i<$li;$i++) {
                    ?>
                            <li><a href="?page=<?php echo $i + $page; ?><?php echo isset($key) ? '&key='.$key : '';?>"><?php echo $i + $page; ?></a></li>
                    <?php        
                        }
                    ?>
                    <li>
                        <a href="?page=<?php echo ($page+1)>=$pages ? $pages : $page+1; ?><?php echo isset($key) ? '&key='.$key : '';?>" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
    
    <!-- 弹窗 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">新建</h4>
                </div>
                <div class="modal-body">
                    <form id="form-add">
                        <input type="hidden" name="id" id="id" value="0">
                        <div class="form-group">
                            <span class="c-red">* </span><label for="truename">姓 名</label>
                            <input type="text" class="form-control" id="truename" name="truename" placeholder="姓 名">
                        </div>
                        <div class="form-group">
                            <label for="organization">所在机构</label>
                            <input type="text" class="form-control" id="organization" name="organization"
                                placeholder="所在机构">
                        </div>
                        <div class="form-group">
                            <label for="position">职 务</label>
                            <input type="text" class="form-control" id="position" name="position" placeholder="职 务">
                        </div>
                        <div class="form-group">
                            <label for="sex">性 别</label>
                            <input type="radio" name="sex" value="1" id="sex1" checked>男
                            <input type="radio" name="sex" value="2" id="sex2">女
                        </div>
                        <div class="form-group">
                            <span class="c-red">* </span><label for="phone">电 话</label>
                            <input type="text" class="form-control" id="phone" name="phone" placeholder="电 话">
                        </div>
                        <div class="form-group">
                            <label for="email">邮 箱</label>
                            <input type="text" class="form-control" id="email" name="email" placeholder="邮 箱">
                        </div>
                        <div class="form-group">
                            <label for="feeType">费用类型</label>
                            <input type="radio" name="feeType" id="feeType1" value="1" checked>普通
                            <input type="radio" name="feeType" id="feeType2" value="2">学生
                        </div>
                        <div class="form-group">
                            <label for="isPay">是否有报告</label>
                            <input type="radio" name="isReport" id="isReport1" value="1">是
                            <input type="radio" name="isReport" id="isReport2" value="0" checked>否
                        </div>
                        <div class="form-group">
                            <label for="report_title">报告题目</label>
                            <input type="text" class="form-control" id="reportTitle" name="reportTitle" placeholder="报告题目">
                        </div>
                        <div class="form-group">
                            <label for="feeType">房 型</label>
                            <input type="radio" name="roomType" id="roomType1" value="1" checked>标准间
                            <input type="radio" name="roomType" id="roomType2" value="2">商务间
                        </div>
                        <div class="form-group">
                            <label for="isPay">是否合住</label>
                            <input type="radio" name="isShare" id="isShare1" value="1">是
                            <input type="radio" name="isShare" id="isShare2" value="0" checked>否
                        </div>
                        <div class="form-group">
                            <label for="isPay">付款方式</label>
                            <input type="radio" name="payType" id="payType1" value="0" checked>汇款
                            <input type="radio" name="payType" id="payType2" value="1">现场缴费
                        </div>
                        <div class="form-group">
                            <label for="isPay">是否缴费</label>
                            <input type="radio" name="isPay" id="isPay1" value="1">是
                            <input type="radio" name="isPay" id="isPay2" value="0" checked>否
                        </div>
                        <div class="form-group">
                            <label for="money">金 额</label>
                            <input type="number" class="form-control" id="money" name="money" placeholder="金额">
                        </div>
                        <!-- 开票信息 -->
                        <div class="form-group">
                            <label>开票类型</label>
                            <div>
                                <label class="radio-inline">
                                    <input type="radio" value="1" name="taxType" id="taxType1" checked  onclick=text(value)>普票
                                </label>
                                <label class="radio-inline">
                                    <input type="radio" value="2" name="taxType" id="taxType2" onclick=text(value)>专票
                                </label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>发票抬头</label>
                            <input type="text" class="form-control" name="taxTitle" placeholder="发票抬头"/>
                        </div>
                        <div class="form-group">
                            <label>纳税人识别号</label>
                                <input type="text" class="form-control" name="taxNumber" placeholder="纳税人识别号"/>
                        </div>
                        <div id='isShow' style="display: none;">
                            <div class="form-group">
                                <label>开户行及账号</label>
                                    <input type="text" class="form-control" name="bankAccount" placeholder="开户行及账号"/>
                            </div>
                            <div class="form-group">
                                <label>地 址</label>
                                <div>
                                    <input type="text" class="form-control" name="companyAddress" placeholder="地 址"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>开户电话</label>
                                    <input type="text" class="form-control" name="companyPhone" placeholder="开户电话"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>邮寄地址</label>
                            <input type="text" class="form-control" name="mailingAddress" placeholder="邮寄地址"/>
                        </div>
                        <div class="form-group">
                            <label >邮 编</label>
                            <input type="text" class="form-control" name="postcode" placeholder="邮 编"/>
                        </div>
                        <!-- 开票信息 end -->
                        <div class="form-group">
                            <label for="remark">备注</label>
                            <textarea id="remark" name="remark" class ="form-control"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="save()">提交</button>
                </div>
            </div><!-- /.modal-content -->
        </div>
    </div><!-- /.modal -->
    <div class="modal fade" id="reModal" tabindex="-1" role="dialog" aria-labelledby="reModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="reModalLabel">修改密码</h4>
                </div>
                <div class="modal-body">
                    <form id="form-repassword">
                        <div class="form-group">
                            <label>原密码</label>
                            <input type="password" class="form-control" name="oldpassword" placeholder="原密码"/>
                        </div>
                        <div class="form-group">
                            <label >新密码</label>
                            <input type="password" class="form-control" name="newpassword" placeholder="新密码"/>
                        </div>
                        <div class="form-group">
                            <label >确认密码</label>
                            <input type="password" class="form-control" name="repassword" placeholder="确认密码"/>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="rePasswd()">提交</button>
                </div>
            </div><!-- /.modal-content -->
        </div>
    </div><!-- /.modal -->

</body>
<script>
    // 退出登陆
    function logout() {
        var r=confirm("纳尼,要退出？")
        if (r==true) {
            window.location.href = "/admin/login/logout";
        }
    }

    function rePasswd() {
        var oldpassword = $('input[name="oldpassword"]').val();
        var newpassword = $('input[name="newpassword"]').val();
        var repassword = $('input[name="repassword"]').val();
        if (isEmpty(oldpassword) || isEmpty(newpassword) || isEmpty(repassword)) {
            alert('原密码新密码不能为空');
            return false;
        }

        if (newpassword!=repassword) {
            alert('新密码不一致');
            return false;
        }
        $.ajax({
            url: "/admin/user/repassword",
            type: "POST",
            data: {'oldpassword': oldpassword, 'newpassword': newpassword},
            dataType: 'json',
            success: function (res) {
                console.log(res);
                alert(res.msg);
                if (res.code == 0) {
                    window.location.href = "/admin/login/logout";
                }
            }
        })
    }

    function search() {
        var key = $("#key").val();
        window.location.href = "?key=" + key;
    }

    function celerSearch() {
        window.location.href = "/";
    }

    // 开票类型
    function text(val) {
        if (val == 2) {
            document.querySelector('#isShow').style.display = 'block'
        } else {
            document.querySelector('#isShow').style.display = 'none'
        }
    }

    function update(id) {
        $("#id").val(id);
        $.ajax({
            url: "/admin/index/info/id/" + id,
            type: "GET",
            data: {},
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if (res.code == 0) {
                    var data = res.data;
                    $("#id").val(data.id);
                    $("#truename").val(data.truename);
                    $("#organization").val(data.organization);
                    $("#position").val(data.position);
                    $('input[name="sex"]').prop('checked', false);
                    if (data.sex==2) {
                        $("#sex2").prop('checked', true);
                    } else {
                        $("#sex1").prop('checked', true);
                    }
                    $("#phone").val(data.phone);
                    $("#email").val(data.email);
                    $('input[name="feeType"]').prop('checked', false);
                    if (data.fee_type==1) {
                        $("#feeType1").prop('checked', true);
                    } else {
                        $("#feeType2").prop('checked', true);
                    }
                    $('input[name="isReport"]').prop('checked', false);
                    if (data.is_report==1) {
                        $("#isReport1").prop('checked', true);
                    } else {
                        $("#isReport").prop('checked', true);
                    }
                    $("#reportTitle").val(data.report_title);
                    $('input[name="roomType"]').prop('checked', false);
                    if (data.room_type==1) {
                        $("#roomType1").prop('checked', true);
                    } else {
                        $("#roomType2").prop('checked', true);
                    }
                    $('input[name="isShare"]').prop('checked', false);
                    if (data.is_share==1) {
                        $("#isShare").prop('checked', true);
                    } else {
                        $("#isShare2").prop('checked', true);
                    }
                    $('input[name="payType"]').prop('checked', false);
                    if (data.pay_type==1) {
                        $("#payType1").prop('checked', true);
                    } else {
                        $("#payType2").prop('checked', true);
                    }
                    // $("#isPay").val(data.isPay);
                    $('input[name="isPay"]').prop('checked', false);
                    if (data.is_pay==1) {
                        $("#isPay1").prop('checked', true);
                    } else {
                        $("#isPay2").prop('checked', true);
                    }
                    $("#money").val(data.money);
                    $('input[name="taxType"]').prop('checked', false);
                    if (data.pay_type==2) {
                        $("#taxType2").prop('checked', true);
                        document.querySelector('#isShow').style.display = 'block';
                    } else {
                        $("#taxType1").prop('checked', true);
                        document.querySelector('#isShow').style.display = 'none';
                    }
                    $('input[name="taxTitle"]').val(data.tax_title);
                    $('input[name="taxNumber"]').val(data.tax_number);
                    $('input[name="bankAccount"]').val(data.bank_account);
                    $('input[name="companyAddress"]').val(data.company_address);
                    $('input[name="companyPhone"]').val(data.company_Phone);
                    $('input[name="mailingAddress"]').val(data.mailing_address);
                    $('input[name="postcode"]').val(data.postcode);
                    $("#remark").val(data.remark);
                    $('#myModal').modal("show");
                } else {
                    alert(res.msg);
                }
                return false;
            }
        });
    }

    function save() {
        var truename = $("#truename").val();
        var phone = $("#phone").val();
        // console.log(truename);
        // console.log(phone);
        if (isEmpty(truename) || isEmpty(phone)) {
            alert('姓名 or 电话不能为空');
            return false;
        }
        
        var data = $('#form-add').serializeArray();
        $.ajax({
            url: "/admin/index/save",
            type: "POST",
            data: data,
            dataType: 'json',
            success: function (res) {
                // console.log(res);
                $("#id").val(0);
                if (res.code == 0) {
                    alert('保存成功');
                    window.location.reload();
                } else {
                    alert(res.msg);
                }

                return false;
            }
        });
    }

    function del(id, that) {
        $.ajax({
            url: "/admin/index/delete",
            type: "POST",
            data: {
                'id': id
            },
            dataType: 'json',
            success: function (res) {
                // console.log(res);
                if (res.code == 0) {
                    alert('删除成功');
                    $(that).parents('tr').remove();
                    // window.location.reload();
                } else {
                    alert(res.msg);
                }

                return false;
            }
        });
    }

    function doPrint(id) {
        $.ajax({
            url: "/admin/index/do_print",
            type: "POST",
            data: {'id': id},
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {
                    // qrcode.clear();
                    var data = res.data;
                    let str = '<div style="margin: 10px auto; text-align: center;">';
                    str += '<img src="' + data.img + '">';
                    str += '<p style="font-size: 14px;margin-top: 10px;"><font style="vertical-align: inherit;">' + data.id + '  ' + data.truename + '</font></p>';
                    str += '<p style="font-size: 14px;"><font style="vertical-align: inherit;">'+data.organization+'</font></p>';
                    str += '</div>';
                    window.document.body.innerHTML = str;
                    // console.log(str);
                    setTimeout(function(){ window.print(); }, 500);
                } else {
                    alert(res.msg);
                }
                return false;
            }
        });
    }
    //定义打印前事件 
    var beforePrint = function () {
        console.log("打印前");
    };
    //定义打印后事件 
    var afterPrint = function () {
        console.log("打印后");
        window.location.reload();
    }
 
    // //监听window状态
    // if (window.matchMedia) {
    //     var mediaQueryList = window.matchMedia('print');
    //     //为印添加事件
    //     mediaQueryList.addListener(function (mql) {
    //         console.log(mql);
    //         if (mql.matches) {
    //             beforePrint();
    //         } else {
    //             afterPrint();
    //         }
    //     });
    // }
 
    //打印前事件
    window.onbeforeprint = beforePrint;
    //打印后事件
    window.onafterprint = afterPrint;

    function printReceipt() {
        var checkbox = $('.data-row input[name="checkbox[]"]');
        var ids = new Array();
        checkbox.each(function (x) {
            if (this.checked)
                ids.push(this.value);
        })
        console.log(ids);
        window.open('/admin/index/print_receipt?ids='+ids);
        return false;
    }   
</script>

</html>